package com.zz.ecommerce.common;

import com.zz.ecommerce.common.bean.TableBean;
import com.zz.ecommerce.utils.BeanMapUtill;
import com.zz.ecommerce.utils.ZzStringUtil;
import org.apache.shiro.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * 生成代码controller
 * controller
 *
 * @author zhangmeng
 * @date 2016/03/28
 */
@Controller
@RequestMapping("/testXXX")
public class TestController {

    private static final Logger LOG = LoggerFactory.getLogger(TestController.class);
    @Autowired
    CommonDao commonDao;
    public static final String FILE_PATH = "F:/code";

    @RequestMapping(value = "/code", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String code(String tableName) throws Exception {
        tableName = tableName.toUpperCase();
        generateBean(getTableInfo(tableName), tableName);
        generateController(getTableInfo(tableName), tableName);
        generateListJsp(getTableInfo(tableName), tableName);
        generateDetailJsp(getTableInfo(tableName), tableName);
        return "success:" + ZzStringUtil.getDefaultDateTime();
    }

    /**
     * 获取表信息
     *
     * @param tableName
     * @return
     * @throws Exception
     */
    List<TableBean> getTableInfo(String tableName) throws Exception {
        List<TableBean> listTableInfo = BeanMapUtill.mapList2BeanList(commonDao.qrySql("select " +
                "a.table_name tableName, a.column_name columnName, a.data_type dataType,a.data_length dataLength,a.data_precision dataPrecision, b.comments" +
                " from user_tab_cols a" +
                " left join user_col_comments b" +
                " on a.TABLE_NAME = b.table_name" +
                " and a.COLUMN_NAME = b.COLUMN_NAME" +
                " where a.table_name = '" + tableName + "'" +
                " order by a.table_name"), TableBean.class);
        return listTableInfo;
    }

    /**
     * 生成bean
     *
     * @param tableInfo
     * @param tableName
     * @throws Exception
     */
    void generateBean(List<TableBean> tableInfo, String tableName) throws Exception {
        if (!CollectionUtils.isEmpty(tableInfo)) {
            String className = ZzStringUtil.capFirst(ZzStringUtil.transLateUnderLine2Upper(tableName));
            String fileName = className + ".java";
            StringBuffer codeContent = new StringBuffer();
            codeContent.append("import com.zz.ecommerce.utils.SqlType;\r\n");
            codeContent.append("public class " + ZzStringUtil.capFirst(ZzStringUtil.transLateUnderLine2Upper(tableName)) + " implements Serializable {\r\n");
            for (TableBean column : tableInfo) {
                String columnName = ZzStringUtil.transLateUnderLine2Upper(column.getColumnName());
                if ("DATE".equals(column.getDataType())) {
                    codeContent.append("@SqlType(type = \"date\", format = \"yyyy-MM-dd hh24:mi:ss\",qryStart = \"" + columnName + "Start\",qryEnd = \"" + columnName + "End\")\r\n");
                    codeContent.append("private String " + columnName + ";//" + column.getComments() + "\r\n");
                    codeContent.append("@SqlType(inTable = false, type = \"date\", format = \"yyyy-MM-dd hh24:mi:ss\")\r\n");
                    codeContent.append("private String " + columnName + "Start;\r\n");
                    codeContent.append("@SqlType(inTable = false, type = \"date\", format = \"yyyy-MM-dd hh24:mi:ss\")\r\n");
                    codeContent.append("private String " + columnName + "End;\r\n");
                } else {
                    codeContent.append("private String " + columnName + ";//" + column.getComments() + "\r\n");
                }
            }
            codeContent.append("}\r\n");
            createCodeFile(fileName, codeContent.toString());
        }
    }

    /**
     * 生成controller
     *
     * @param tableInfo
     * @param tableName
     * @throws Exception
     */
    void generateController(List<TableBean> tableInfo, String tableName) throws Exception {
        if (!CollectionUtils.isEmpty(tableInfo)) {
            String className = ZzStringUtil.capFirst(ZzStringUtil.transLateUnderLine2Upper(tableName));
            String fileName = className + "Controller.java";
            StringBuffer codeContent = new StringBuffer();
            codeContent.append("import com.zz.ecommerce.common.CommonDao;\r\n" +
                    "import com.zz.ecommerce.common.GridRequest;\r\n" +
                    "import com.zz.ecommerce.common.GridResult;\r\n" +
                    "import org.springframework.beans.factory.annotation.Autowired;\r\n" +
                    "import org.springframework.stereotype.Controller;\r\n" +
                    "import org.springframework.web.bind.annotation.RequestMapping;\r\n" +
                    "import org.springframework.web.bind.annotation.RequestMethod;\r\n" +
                    "import org.springframework.web.bind.annotation.ResponseBody;\r\n" +
                    "import org.springframework.ui.ModelMap;\r\n" +
                    "import javax.servlet.http.HttpSession;\r\n" +
                    "import java.util.List;\r\n");
            codeContent.append("@Controller\r\n");
            codeContent.append("@RequestMapping(\"/" + className + "\")\r\n");
            codeContent.append("public class " + className + "Controller{\r\n");
            codeContent.append("@Autowired\r\n");
            codeContent.append("CommonDao commonDao;\r\n");
            //跳转到列表页
            codeContent.append("@RequestMapping(value = \"" + className + "Manager\", method = RequestMethod.GET)\r\n" +
                    " public String " + className + "Manager() {\r\n" +
                    " return \"" + className.toLowerCase() + "_manager\";\r\n" +
                    " }");
            //列表查询
            codeContent.append("@RequestMapping(value = \"qry" + className + "Page\", method = {RequestMethod.GET, RequestMethod.POST})\r\n" +
                    " @ResponseBody\r\n" +
                    " public GridResult<" + className + "> qry" + className + "Page(GridRequest gridRequest, " + className + " " + ZzStringUtil.transLateUnderLine2Upper(tableName) + ") {\r\n" +
                    " List<" + className + "> list = commonDao.pageWhere(\"" + tableName + "\", " + ZzStringUtil.transLateUnderLine2Upper(tableName) + ", gridRequest.getStart(), gridRequest.getEnd(), \"\", \"\");\r\n" +
                    " long count = commonDao.countWhere(\"" + tableName + "\", " + ZzStringUtil.transLateUnderLine2Upper(tableName) + ",\"\");\r\n" +
                    " return new GridResult<" + className + ">(list, gridRequest, count);\r\n" +
                    " }\r\n");
            //保存内容
            codeContent.append("@RequestMapping(value = \"save" + className + "\", method = {RequestMethod.GET, RequestMethod.POST})\r\n" +
                    " @ResponseBody\r\n" +
                    " public String save" + className + "(HttpSession session, " + className + " " + ZzStringUtil.transLateUnderLine2Upper(tableName) + ") {\r\n" +
                    " return \"success\";\r\n" +
                    " }\r\n");
            //跳转到详情页
            codeContent.append("@RequestMapping(value = \"get" + className + "Detail\", method = {RequestMethod.GET, RequestMethod.POST})\r\n" +
                    " public String get" + className + "Detail(" + className + " " + ZzStringUtil.transLateUnderLine2Upper(tableName) + ",ModelMap modelMap) {\r\n" +
                    " " + className + " " + ZzStringUtil.transLateUnderLine2Upper(tableName) + "Detail = (" + className + ")commonDao.qryOneCommon(\"" + tableName + "\", " + ZzStringUtil.transLateUnderLine2Upper(tableName) + ",\"\");\r\n" +
                    " modelMap.put(\"" + ZzStringUtil.transLateUnderLine2Upper(tableName) + "\"," + ZzStringUtil.transLateUnderLine2Upper(tableName) + "Detail);\r\n" +
                    " return \"" + className.toLowerCase() + "_detail\";\r\n" +
                    " }\r\n");
            codeContent.append("}\r\n");
            createCodeFile(fileName, codeContent.toString());
        }
    }

    /**
     * 生成列表页
     *
     * @param tableInfo
     * @param tableName
     * @throws Exception
     */
    void generateListJsp(List<TableBean> tableInfo, String tableName) throws Exception {

        if (!CollectionUtils.isEmpty(tableInfo)) {
            String className = ZzStringUtil.capFirst(ZzStringUtil.transLateUnderLine2Upper(tableName));
            String fileName = className.toLowerCase() + "_manager.jsp";
            StringBuffer codeContent = new StringBuffer();
            //生成页面头
            codeContent.append("<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\" %>\r\n" +
                    "<!DOCTYPE html>\r\n" +
                    "<html>\r\n" +
                    "<head>\r\n" +
                    " <title>XXXXXX</title>\r\n" +
                    " <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'/>\r\n" +
                    " <meta http-equiv=\"pragma\" content=\"no-cache\">\r\n" +
                    " <meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n" +
                    " <meta http-equiv=\"expires\" content=\"0\">\r\n" +
                    " <%@include file=\"/WEB-INF/pages/common/taglib.jsp\" %>\r\n" +
                    " <%@include file=\"/WEB-INF/pages/common/common.jsp\" %>\r\n" +
                    "</head>\r\n" +
                    "<body>\r\n" +
                    "<div class=\"row-fluid\" id=\"content-wrapper\">\r\n" +
                    " <div class='span12'>\r\n" +
                    " <div class=\"row-fluid\">\r\n" +
                    " <div class=\"span12\">\r\n" +
                    " <div class=\"page-header\">\r\n" +
                    " <h1 class=\"pull-left\">\r\n" +
                    " <i class=\"icon-table\"></i><span id=\"spanChange\">XXXXXX</span>\r\n" +
                    " </h1>\r\n" +
                    "\r\n" +
                    " <div class=\"pull-right\">\r\n" +
                    " <ul class=\"breadcrumb\">\r\n" +
                    " <li><a><i class=\"icon-home\"></i></a></li>\r\n" +
                    " <li class=\"separator\"><i class=\"icon-angle-right\"></i></li>\r\n" +
                    " <li class=\"active\">XXXXXX</li>\r\n" +
                    " <li class=\"separator\"><i class=\"icon-angle-right\"></i></li>\r\n" +
                    " <li class=\"active\">XXXXXX</li>\r\n" +
                    " </ul>\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    " <div class='row-fluid'>\r\n" +
                    " <div class='span12 box bordered-box orange-border' style='margin-bottom: 0;'>\r\n" +
                    " <div class='row-fluid'>\r\n" +
                    " <div class='span12 box'>\r\n" +
                    " <div class='box-header blue-background'>\r\n" +
                    " <div class='title'>\r\n" +
                    " <i class='icon-edit'></i>\r\n" +
                    " 基本查询信息\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    " <div class='box-content'>\r\n" +
                    " <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin-bottom: 10px;\r\n" +
                    " border-bottom: 1px solid #eee;\">\r\n" +
                    " <tr>\r\n" +
                    " <td style=\"text-align: left; padding-right: 10px; padding-left: 10px;\">\r\n" +
                    " <button id=\"btnSearch\" class='btn btn-primary btn-small'\r\n" +
                    " style='margin-bottom: 5px'>\r\n" +
                    " <i class='icon-search'></i>查 询\r\n" +
                    " </button>\r\n" +
                    " <button id=\"btnAdd\" class='btn btn-primary btn-small'\r\n" +
                    " style='margin-bottom: 5px'>\r\n" +
                    " <i class='icon-plus'></i>新增\r\n" +
                    " </button>\r\n" +
                    " <button id=\"btnModify\" class='btn btn-primary btn-small'\r\n" +
                    " style='margin-bottom: 5px'>\r\n" +
                    " <i class='icon-edit'></i>修 改\r\n" +
                    " </button>\r\n" +
                    " <button id=\"btnDelete\" class='btn btn-primary btn-small'\r\n" +
                    " style='margin-bottom: 5px'>\r\n" +
                    " <i class='icon-remove'></i>删 除\r\n" +
                    " </button>\r\n" +
                    " <button class='btn btn-primary btn-small'\r\n" +
                    " onclick=\"ClearAllInput('SearchTable')\"\r\n" +
                    " style='margin-bottom: 5px'>\r\n" +
                    " <i class='icon-trash'></i>重 置\r\n" +
                    " </button>\r\n" +
                    " </td>\r\n" +
                    " </tr>\r\n" +
                    " </table>\r\n" +
                    " <table id=\"SearchTable\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">\r\n" +
                    " <tr>\r\n");
            //生成查询条件
            for (int i = 0; i < tableInfo.size(); i++) {
                TableBean column = tableInfo.get(i);
                String colName = ZzStringUtil.transLateUnderLine2Upper(column.getColumnName());
                if ((i + 1) % 4 == 0) {
                    codeContent.append("</tr><tr>\r\n");
                }
                codeContent.append(" <td style=\"width: 25%;\">\r\n" +
                        " <table width=\"100%\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\">\r\n" +
                        " <tr>\r\n" +
                        " <td class=\"titleCss\">\r\n");
                if ("DATE".equals(column.getDataType())) {
                    codeContent.append("" + column.getComments() + "\r\n" +
                            " </td>\r\n" +
                            " <td class=\"tdCss\">\r\n" +
                            " <input type=\"text\" style=\"width: 100%;\" class=\"Wdate\"" +
                            " onclick=\"WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'#F{$dp.$D(\\'" + colName + "End\\')}'})\"\r\n" +
                            " timeformat=\"YYYY-MM-DD HH:MM:SS\" id=\"" + colName + "Start\"\r\n" +
                            " datafield=\"SearchTable." + colName + "Start\"/></td></tr></table></td>\r\n");
                    codeContent.append(" <td style=\"width: 25%;\">\r\n" +
                            " <table width=\"100%\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\">\r\n" +
                            " <tr>\r\n" +
                            " <td class=\"titleCss\">\r\n");
                    codeContent.append("至\r\n" +
                            " </td>\r\n" +
                            " <td class=\"tdCss\">\r\n" +
                            " <input type=\"text\" style=\"width: 100%;\" class=\"Wdate\"" +
                            " onclick=\"WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'#F{$dp.$D(\\'" + colName + "End\\')}'})\"\r\n" +
                            " timeformat=\"YYYY-MM-DD HH:MM:SS\" id=\"" + colName + "End\"\r\n" +
                            " datafield=\"SearchTable." + colName + "End\"/></td></tr></table></td>\r\n");
                } else {
                    codeContent.append("" + column.getComments() + "\r\n" +
                            " </td>\r\n" +
                            " <td class=\"tdCss\">\r\n" +
                            " <input type=\"text\" datafield=\"SearchTable." + colName + "\"/>\r\n" +
                            " </td>\r\n" +
                            " </tr>\r\n" +
                            " </table>\r\n" +
                            " </td>\r\n");
                }

            }
            codeContent.append("</tr>\r\n" +
                    " </table>\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    " <div class='responsive-table' >\r\n" +
                    " <table id=\"corptable\" style=\"height: 1px\">\r\n" +
                    " </table>\r\n" +
                    " <div id=\"corptablePage\">\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    " </div>\r\n" +
                    "</div>\r\n" +
                    "<script type=\"text/javascript\">\r\n" +
                    "\r\n" +
                    " function urlCode(cellvalue, options, rowObject) {\r\n" +
                    " cellvalue = cellvalue || '';\r\n" +
                    " cellvalue = $.trim(cellvalue);\r\n" +
                    "\r\n" +
                    " var url = '${basePath}/get" + className + "Detail?modify=false&id=' + rowObject.id;\r\n" +
                    " return \"<a class='btn-link' href=\\\"\" + url + \"\\\">\" + \"<span style='color:#00acec'>\" + cellvalue + \"</span></a>\";\r\n" +
                    " }\r\n" +
                    "\r\n" +
                    " $(function () {\r\n" +
                    " $(\"#corptable\").jqGrid({\r\n" +
                    " datatype: \"json\",\r\n" +
                    " url: $basePath + \"/qry" + className + "Page\",\r\n" +
                    " colNames: [\r\n");
            for (TableBean column : tableInfo) {
                codeContent.append(" '" + column.getComments() + "',\r\n");
            }
            codeContent.append(
                    " ],\r\n" +
                            " colModel: [\r\n");
            for (TableBean column : tableInfo) {
                String colName = ZzStringUtil.transLateUnderLine2Upper(column.getColumnName());
                codeContent.append(" {name: '" + colName + "', index: '" + colName + "'},\r\n");
            }

            codeContent.append(" ],\r\n");
            codeContent.append(" viewrecords: true,\r\n" +
                    " autowidth: true,\r\n" +
                    " height: 'auto',\r\n" +
                    " shrinkToFit: false,\r\n" +
                    " autoScroll: true,\r\n" +
                    " rowNum: 10, //每页显示记录数\r\n" +
                    " rowList: [10, 20, 100], //可调整每页显示的记录数\r\n" +
                    " rownumWidth: 35,\r\n" +
                    " multiselect: true,\r\n" +
                    "mtype: 'POST',\r\n" +
                    " width: $('body').width(),\r\n" +
                    " pager: \"#corptablePage\", //分页工具栏\r\n" +
                    " loadComplete: function () {\r\n" +
                    " var rowNum = $(this).jqGrid('getGridParam', 'records');\r\n" +
                    " if (rowNum <= 0) {\r\n" +
                    " if ($(this).parent().children('.norecords').length == 0)\r\n" +
                    " $(this).parent().append(\"<div class=\\\"norecords\\\">暂无数据！</div>\");\r\n" +
                    " $(this).parent().children('.norecords').show();\r\n" +
                    " }\r\n" +
                    " else {//如果存在记录，则隐藏提示信息。\r\n" +
                    " $(this).parent().children('.norecords').hide();\r\n" +
                    " }\r\n" +
                    "\r\n" +
                    " $(\"#corptable\").setGridWidth($(window).width() - 2);\r\n" +
                    " $(\".ui-jqgrid tr.ui-row-ltr td\").css('text-align', 'center');\r\n" +
                    " }\r\n" +
                    " });\r\n" +
                    " });\r\n" +
                    "\r\n" +
                    " function reloadGrid() {\r\n" +
                    " var jsonStr = customFormToData(\"SearchTable\");\r\n" +
                    " $(\"#corptable\").jqGrid('setGridParam', {\r\n" +
                    " url: $basePath + \"/declare/queryDeclareList\",\r\n" +
                    " postData: JSON.parse(jsonStr), //发送数据\r\n" +
                    " mtype: 'POST',\r\n" +
                    " page: 1\r\n" +
                    " }).trigger(\"reloadGrid\"); //重新载入\r\n" +
                    " }\r\n" +
                    "\r\n" +
                    " $(function () {\r\n" +
                    " $(\"#btnSearch\").bind(\"click\", function () {\r\n" +
                    " reloadGrid();\r\n" +
                    " });\r\n" +
                    "\r\n" +
                    "$(\"#btnModify\").bind(\"click\", function () {\r\n" +
                    "            var selectedIDs = $(\"#corptable\").getGridParam(\"selarrrow\");\r\n" +
                    "            if (0 == selectedIDs.length) {\r\n" +
                    "                ShowMessages(\"对不起,您必须先选择一条记录\", \"系统提示\", \"msgwaring\", null, 0);\r\n" +
                    "            } else {\r\n" +
                    "                if (1 != selectedIDs.length) {\r\n" +
                    "                    ShowMessages(\"对不起,您只能选中一条记录\", \"系统提示\", \"msgwaring\", null, 0);\r\n" +
                    "                } else {\r\n" +
                    "                    location.href = \"${basePath}/get" + className + "Detail?sysId=\" + selectedIDs[0];\r\n" +
                    "                }\r\n" +
                    "            }\r\n" +
                    "        });\r\n" +
                    "$(\"#btnAdd\").bind(\"click\", function () {\r\n" +
                    "            location.href = \"${basePath}/get" + className + "Detail?id=\";\r\n" +
                    "        });\r\n" +
                    "$(\"#btnDelete\").bind(\"click\", function () {\r\n" +
                    "            var selectedIDs = $(\"#corptable\").getGridParam(\"selarrrow\");\r\n" +
                    "            if (0 == selectedIDs.length) {\r\n" +
                    "                ShowMessages(\"对不起,您必须先选择一条记录\", \"系统提示\", \"msgwaring\", null, 0);\r\n" +
                    "            } else {\r\n" +
                    "                showConfirm(\"确定要删除选中的记录吗?\", \"系统提示\", function () {\r\n" +
                    "                    $.ajax({\r\n" +
                    "                        type: \"post\",\r\n" +
                    "                        async: false,\r\n" +
                    "                        url: $basePath + \"/delete" + className + "\",\r\n" +
                    "                        data: {sysIds: array2Str(selectedIDs)},\r\n" +
                    "                        success: function (result) {\r\n" +
                    "                            if (result) {\r\n" +
                    "                                showOKMessage('删除成功!', '', function () {\r\n" +
                    "                                    reloadGrid();\r\n" +
                    "                                });\r\n" +
                    "                            } else {\r\n" +
                    "                                ShowMessages(\"删除失败\", \"系统提示\", \"msgwaring\", null, 0);\r\n" +
                    "                            }\r\n" +
                    "                        }\r\n" +
                    "                    });\r\n" +
                    "                })\r\n" +
                    "            }\r\n" +
                    "        });\r\n" +
                    "\r\n" +
                    " $(\"#btnReset\").bind(\"click\", function () {\r\n" +
                    " document.forms[0].reset();\r\n" +
                    " });\r\n" +
                    " });\r\n" +
                    "\r\n" +
                    "</script>\r\n" +
                    "</body>\r\n" +
                    "</html>\r\n");
            createCodeFile(fileName, codeContent.toString());
        }

    }

    /**
     * 生成详情页
     *
     * @param tableInfo
     * @param tableName
     * @throws Exception
     */
    void generateDetailJsp(List<TableBean> tableInfo, String tableName) throws Exception {
        if (!CollectionUtils.isEmpty(tableInfo)) {
            String className = ZzStringUtil.capFirst(ZzStringUtil.transLateUnderLine2Upper(tableName));
            String objName = ZzStringUtil.transLateUnderLine2Upper(tableName);
            String fileName = className.toLowerCase() + "_detail.jsp";
            StringBuffer codeContent = new StringBuffer();
            codeContent.append("<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\" %>\r\n" +
                    "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n" +
                    "<html>\r\n" +
                    "<head>\r\n" +
                    "    <meta charset=\"utf-8\">\r\n" +
                    "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\r\n" +
                    "    <title>XXXXX</title>\r\n" +
                    "    <%@include file=\"/WEB-INF/pages/common/taglib.jsp\" %>\r\n" +
                    "    <%@include file=\"/WEB-INF/pages/common/common.jsp\" %>\r\n" +
                    "    <style type=\"text/css\">\r\n" +
                    "        .popover {\r\n" +
                    "            max-width: 500px;\r\n" +
                    "            width: auto;\r\n" +
                    "        }\r\n" +
                    "    </style>\r\n" +
                    "\r\n" +
                    "</head>\r\n" +
                    "<body style=\"overflow-x: hidden\">\r\n" +
                    "<form id=\"detailForm\">\r\n" +
                    "    <div class=\"row-fluid box\">\r\n" +
                    "        <div class=\"span12 box-header\">\r\n" +
                    "            <div class=\"title span12\">\r\n" +
                    "                <div style=\"float:left\">\r\n" +
                    "                    <i class=\"icon-edit\" style=\"color: #00F;\">\r\n" +
                    "                    </i>\r\n" +
                    "                    <strong>XXXXX</strong>\r\n" +
                    "                </div>\r\n" +
                    "                <div style=\"float:right\">\r\n" +
                    "                    <button class=\"btn btn-primary btn-small\" type=\"button\" onclick=\"javascript:save()\">\r\n" +
                    "                        <i class=\"icon-save\"></i>保存\r\n" +
                    "                    </button>\r\n" +
                    "                    <button class=\"btn btn-primary btn-small\" type=\"button\" onclick=\"javascript:history.go(-1)\">\r\n" +
                    "                        <i class=\"icon-reply-all\"></i>返回\r\n" +
                    "                    </button>\r\n" +
                    "                </div>\r\n" +
                    "            </div>\r\n" +
                    "        </div>\r\n" +
                    "        <div class=\"span12 box-content\">\r\n");
            for (TableBean column : tableInfo) {
                String colName = ZzStringUtil.transLateUnderLine2Upper(column.getColumnName());
                String coments = column.getComments();
                codeContent.append("            <div class=\"span3\">\r\n" +
                        "                <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\r\n" +
                        "                    <tbody>\r\n" +
                        "                    <tr>\r\n" +
                        "                        <td class=\"titleCss100\">\r\n" +
                        "                            " + coments + "\r\n" +
                        "                        </td>\r\n" +
                        "                        <td class=\"tdCss\">\r\n" +
                        "                            <input type=\"text\" name=\"" + colName + "\"\r\n" +
                        "                                   value=\"${" + objName + "." + colName + "}\">\r\n" +
                        "                        </td>\r\n" +
                        "                    </tr>\r\n" +
                        "                    </tbody>\r\n" +
                        "                </table>\r\n" +
                        "            </div>\r\n");
            }
            codeContent.append("    </div>\r\n</div>\r\n" +
                    "</form>\r\n" +
                    "<script type=\"text/javascript\">\r\n" +
                    "function save() {\r\n" +
                    "        $.ajax({\r\n" +
                    "            type: \"post\",\r\n" +
                    "            async: false,\r\n" +
                    "            url: $basePath + \"/save" + className + "\",\r\n" +
                    "            data: form2Json(\"detailForm\"),\r\n" +
                    "            success: function (result) {\r\n" +
                    "                if (result) {\r\n" +
                    "                    if (\"success\" == result) {\r\n" +
                    "                        showOKMessage('保存成功!', '', function () {\r\n" +
                    "                        });\r\n" +
                    "                    } else {\r\n" +
                    "                        ShowMessages(result, \"系统提示\", \"msgwaring\", null, 0);\r\n" +
                    "                    }\r\n" +
                    "                } else {\r\n" +
                    "                    ShowMessages(\"保存失败\", \"系统提示\", \"msgwaring\", null, 0);\r\n" +
                    "                }\r\n" +
                    "            }\r\n" +
                    "        });\r\n" +
                    "    }" +
                    "\r\n" +
                    "    $(function () {\r\n" +
                    "        $(\"#detailForm input,textarea,#btnSave,select\").attr(\"disabled\", ${modify});\r\n" +
                    "    });\r\n" +
                    "\r\n" +
                    "\r\n" +
                    "</script>\r\n" +
                    "</body>\r\n" +
                    "</html>\r\n");
            createCodeFile(fileName, codeContent.toString());
        }
    }


    public boolean createCodeFile(String fileName, String content)
            throws Exception {

        File dir = new File(FILE_PATH);
        if (!dir.isDirectory()) {
            dir.mkdirs();
        }
        String fullFileName = FILE_PATH + "/" + fileName;
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(new File(fullFileName));
            outStream.write(content.getBytes("utf-8"));
            outStream.close();
        } catch (IOException e) {
            System.err.println("保存文件失败:" + e.getMessage());
            if (null != outStream) {
                outStream.close();
            }
            throw e;
        }
        return true;
    }
}
